package api;

import com.alibaba.fastjson.JSON;
import domain.Notice;
import domain.Product;
import exception.LoginException;
import service.ProductService;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class HotWeekServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 分层：api：接受请求，提取请求携带参数，返回消息，service：业务处理，dao：数据库访问
        ProductService productServicect = new ProductService();
        Response build=null;
        List<Object []> list=null;
        try {
            list =productServicect.getHotWeekProducts();
            build = Response.build(ResponseCode.SUCCESS,list);
        } catch (SQLException e) {
            e.printStackTrace();
            build = Response.build(ResponseCode.ERROR_DB);
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }
}
